package com.ff.javamonkey.util;

import java.util.ArrayList;

import android.util.Log;

public class StringUtils {
	// Debugging
	private static final String TAG = "StringUtils";

	private StringUtils() {
		// utilitc lass
	}
	
	public static String[] arraySplit(String s, char separatorChar, boolean trim) {
		if (s == null) {
			return null;
		}
		int length = s.length();
		if (length == 0) {
			return new String[0];
		}
		ArrayList<String> list = new ArrayList<String>();
		StringBuilder buff = new StringBuilder(length);
		for (int i = 0; i < length; i++) {
			char c = s.charAt(i);
			if (c == separatorChar) {
				String e = buff.toString();
				list.add(trim ? e.trim() : e);
				buff.setLength(0);
			}
			else if (c == '\\' && i < length - 1) {
				buff.append(s.charAt(++i));
            } else {
                buff.append(c);
            }
        }
        String e = buff.toString();
        list.add(trim ? e.trim() : e);
        String[] array = new String[list.size()];
        list.toArray(array);
        return array;
	}
	
	/**
     * A simple helper menu to obtain a delta between now and current time.
     * @return String to represents the delta.
     */
	public static String getTimeDelta(long now) {
		long delta = System.currentTimeMillis() - now;
		// Zero hour, zero minutes
		int min = (int)((delta/1000)/60);
		int hour = (int)((delta/1000)/3600);
		int seconds = (int)((delta/1000)%60);
		if (min == 0 && hour == 0) {
			return String.format("%d sec",seconds);
		}
		else if (hour == 0) {
			return String.format("%d min, %d sec", min, seconds);
		}
		else 
			return String.format("%d hour, %d min, %d sec", hour, min, seconds);
	}
	
	/**
	 * Given a size in integer, return a proper reading for file size. In KB, or in MB.
	 * @param size
	 * @return
	 */
	public static String getProperFileSize(int size) {
		int kilobytes = size >> 10;
		int megabytes = size >> 20;
//			Log.i(TAG, "Size in Kilobytes " + kilobytes + ", and in Megabytes " + megabytes);
		return null;
	}
}
